package cn.edu.scau.cmi.yangqing.hibernate.client.control;


import java.net.URL;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.yangqing.factory.Creator;
import cn.edu.scau.cmi.yangqing.factory.CreatorPICC;
import cn.edu.scau.cmi.yangqing.factory.CreatorPingan;
import cn.edu.scau.cmi.yangqing.factory.Insurance;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.domain.StudentProperty;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Tutor;
import cn.edu.scau.cmi.yangqing.hibernate.domain.TutorProperty;
import cn.edu.scau.cmi.yangqing.hibernate.service.StudentService;
import cn.edu.scau.cmi.yangqing.hibernate.service.TutorService;
import cn.edu.scau.cmi.yangqing.hibernate.util.StageManage;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

public class tutorControl implements Initializable {

	 	@FXML
	   private TableView<StudentProperty> studentTable;
	   
	   @FXML
	   private TableColumn<StudentProperty, Integer> ids;
	   
	   @FXML
	   private TableColumn<StudentProperty, String> names;
	   
	   @FXML
	   private TableView<StudentProperty> studentTableNo;
	   
	   @FXML
	   private TableColumn<StudentProperty, Integer> idsNo;
	   
	   @FXML
	   private TableColumn<StudentProperty, String> namesNo;
	   
	  
	   @FXML
	   private TextField studentId;
	   @FXML 
	   private Button choiceButton;
	   
	   @FXML
	   private TextField studentIdNo;
	   @FXML 
	   private Button choiceNoButton;
	   
	   private final ObservableList<StudentProperty> cellData = FXCollections.observableArrayList();
	   private final ObservableList<StudentProperty> cellDataNo = FXCollections.observableArrayList();
	   
	   StudentService studentService = new StudentService();
	   TutorService tutorService = new TutorService();
	   
	   Integer id = Integer.valueOf(StageManage.id);
	   
	   public void initialize(URL location, ResourceBundle resources) {
		   //StudentService studentService = new StudentService();
		   List<Student> studentList = tutorService.teachStudentList(id);
		   for (Student student : studentList) {
			   IntegerProperty idProperty = new SimpleIntegerProperty(student.getId());
			   StringProperty nameProperty = new SimpleStringProperty(student.getName());
			   IntegerProperty tutorProperty = new SimpleIntegerProperty(student.getTutor().getId());
			   StudentProperty sProperty = new StudentProperty(idProperty, nameProperty, tutorProperty);
			   cellData.add(sProperty);
		   } 
		   //（2）配置 TableColumn 列
		   ids.setCellValueFactory(cellData -> cellData.getValue().getId().asObject());
		   names.setCellValueFactory(cellData -> cellData.getValue().getName());
//		   tutorids.setCellValueFactory(cellData -> cellData.getValue().getIdt().asObject());
		   //（3）为 TableView 添加数据
		   studentTable.setItems(cellData);
		   
		   List<Student> studentListNo = tutorService.teachNoStudentList(id);
		   for (Student student : studentListNo) {
			   IntegerProperty idProperty = new SimpleIntegerProperty(student.getId());
			   StringProperty nameProperty = new SimpleStringProperty(student.getName());
			   IntegerProperty tutorProperty = new SimpleIntegerProperty(student.getTutor().getId());
			   StudentProperty sProperty = new StudentProperty(idProperty, nameProperty, tutorProperty);
			   cellDataNo.add(sProperty);
		   } 
		   idsNo.setCellValueFactory(cellDataNo -> cellDataNo.getValue().getId().asObject());
		   namesNo.setCellValueFactory(cellDataNo -> cellDataNo.getValue().getName());
		   studentTableNo.setItems(cellDataNo);
	   }

	   public void ChoiceTheStudent(ActionEvent event) {
		   Integer ids = Integer.valueOf(studentId.getText());
		   tutorService.teachStudent(id, ids);
	   }
	   public void ChoiceNoStudent(ActionEvent event) {
		   Integer ids = Integer.valueOf(studentIdNo.getText());
		   tutorService.teachNoStudent(ids);
	   }
}
